package Promotion;

import java.util.ArrayList;
import java.util.List;

// 商品基类
abstract class Goods {
    private String brand;
    private String name;
    private double price;
    private Promotion promotion;

    public Goods(String brand, String name, double price) {
        this.brand = brand;
        this.name = name;
        this.price = price;
    }

    public double getPrice() {
        return price;
    }

    public void setPromotion(Promotion promotion) {
        this.promotion = promotion;
    }

    public double getSalePrice() {
        if (promotion != null) {
            return promotion.calculatePrice(this.price);
        }
        return this.price;
    }

    public String getPromotionDescription() {
        if (promotion != null) {
            return promotion.getPromotionDescription();
        }
        return "无促销活动";
    }

    @Override
    public String toString() {
        return "商品名称:" + name + " 商品价格:" + price + " 商品售价:" + getSalePrice() +
                " 促销条件: " + getPromotionDescription();
    }
}

// 商品子类：鞋
class Shoes extends Goods {
    public Shoes(String brand, String name, double price) {
        super(brand, name, price);
    }
}

// 商品子类：服装
class Clothes extends Goods {
    public Clothes(String brand, String name, double price) {
        super(brand, name, price);
    }
}

// 促销策略接口
interface Promotion {
    double calculatePrice(double price);

    String getPromotionDescription();
}

// 促销策略：折扣
class Discount implements Promotion {
    private double discountRate;

    public Discount(double discountRate) {
        this.discountRate = discountRate;
    }

    @Override
    public double calculatePrice(double price) {
        return price * discountRate;
    }

    @Override
    public String getPromotionDescription() {
        return "打折 " + (discountRate * 100) + "%";
    }
}

// 促销策略：满减
class ThresholdReduce implements Promotion {
    private double threshold;
    private double reduceAmount;
    private double total;

    public ThresholdReduce(double threshold, double reduceAmount) {
        this.threshold = threshold;
        this.reduceAmount = reduceAmount;
    }

    public void addTotal(double amount) {
        this.total += amount;
    }

    public double accounting() {
        System.out.println("nike total: ");
        return (total >= threshold) ? reduceAmount : 0;


    }

    @Override
    public double calculatePrice(double price) {
        addTotal(price);
        return price; // 满减后的价格计算在外部处理
    }

    @Override
    public String getPromotionDescription() {
        return "满" + threshold + "减" + reduceAmount;
    }
}

// 客户类
class Customer {
    private String name;
    private int age;
    private List<Goods> cart;

    public Customer(String name, int age) {
        this.name = name;
        this.age = age;
        this.cart = new ArrayList<>();
    }

    public void put(Goods goods) {
        cart.add(goods);
        System.out.println(goods);
    }

    public double checkout() {
        double total = 0;
        for (Goods goods : cart) {
            total += goods.getSalePrice();
        }
        return total;
    }
}

// 主程序
public class PromotionApplication {
    public static void main(String[] args) {
        Customer customer = new Customer("zhangsan", 10);

        // 商品 1：运动鞋，打9折
        Goods lining = new Shoes("李宁", "运动鞋", 100);
        lining.setPromotion(new Discount(0.9));
        customer.put(lining);

        // 商品 2：篮球鞋，打9折
        lining = new Shoes("李宁", "篮球鞋", 120);
        lining.setPromotion(new Discount(0.9));
        customer.put(lining);

        // 商品 3：跑步鞋，满299减50
        Goods nike = new Shoes("耐克", "跑步鞋", 400);
        ThresholdReduce thresholdReduce = new ThresholdReduce(299, 50);
        nike.setPromotion(thresholdReduce);
        customer.put(nike);

        // 商品 4：西服，满1000减100
        ThresholdReduce thresholdReduce1 = new ThresholdReduce(1000, 100);
        Goods ruomeng = new Clothes("罗蒙", "西服", 1200);
        ruomeng.setPromotion(thresholdReduce1);
        customer.put(ruomeng);

        // 结算
        double account = customer.checkout();
        System.out.printf("满减后应付金额：%.2f\n", account - thresholdReduce.accounting() - thresholdReduce1.accounting());
        System.out.printf("应付金额：%.2f\n", account);
    }
}
